package com.kira.scaffoldmvc.test;

public class ThreadLoopPrint {

    private static final String str = "ABC";

    private static int count = 0;

    private static final int printTime = 3;

    private static final Object lock = new Object();


    public static void main(String[] args) {

        for (int i = 0; i < str.length(); i++) {
            final int threadIndex = i;
            new Thread(() -> {
                for (int j = 0; j < printTime; j++) {
                    synchronized (lock) {
                        while (count % str.length() != threadIndex) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        System.out.println(str.charAt(threadIndex));
                        count++;
                        lock.notifyAll();
                    }
                }
            }).start();
        }

    }


}
